package com.gitee.candy.file;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：suyanlong
 * @date ：Created in 2020/9/11 16:43
 * @description： CSV文件读取与写入
 * @version: V1.0
 * 1.读取csv文件
 * 2.写入Csv文件数据
 */
public class CsvUtils {

    private static int maxColumnNumber = 0;
    private static int maxColumnRowNumber = 0;


    /**
     * 读取csv文件
     * 【去除空格】【格式按第一行对齐】
     *
     * @param filePath
     * @return 二维数组List<List < String>>一维是行数据二维是列数据
     */
    public static List<List<String>> readCsv(String filePath) throws IOException {
        return readCsvs(filePath, false, false, "gbk");
    }

    public static List<List<String>> readCsv(String filePath, boolean skipFirstRow) throws IOException {
        return readCsvs(filePath, skipFirstRow, false, "gbk");
    }

    public static List<List<String>> readCsv(String filePath, boolean skipFirstRow, boolean removeBlank) throws IOException {
        return readCsvs(filePath, skipFirstRow, removeBlank, "gbk");
    }

    public static List<List<String>> readCsv(String filePath, boolean skipFirstRow, boolean removeBlank, String charset) throws IOException {
        return readCsvs(filePath, skipFirstRow, removeBlank, charset);
    }

    /**
     * @param filePath     文件路径
     * @param skipFirstRow 是否跳过第一行
     * @param removeBlank  是否移除空格
     * @param charset      字符集
     * @return
     * @throws IOException
     */
    private static List<List<String>> readCsvs(String filePath, boolean skipFirstRow, boolean removeBlank, String charset) throws IOException {
        List<List<String>> lists = new ArrayList<>();
        //换成你的文件名
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charset));
        int rowNumber = 0;
        if (skipFirstRow) {
            reader.readLine();// 第一行信息，为标题信息，不用,如果需要，注释掉
            rowNumber++;
        }
        String line = null;
        while ((line = reader.readLine()) != null) {
            String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
            List<String> list = new ArrayList<>();
            if (item.length > maxColumnNumber) {
                maxColumnNumber = item.length;
                maxColumnRowNumber = rowNumber;
            }
            for (int i = 0; i < item.length; i++) {
                String s = item[i];
                list.add(removeBlank ? s.replaceAll(" ", "") : s); // 去除空格
            }
            lists.add(list);
            rowNumber++;
        }
        return lists;
    }

    /**
     * 写入Csv文件数据
     *
     * @param filePath 文件路径
     * @param data     写出数据
     */
    public static void writeCsv(String filePath, List<List<String>> data) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true)); // 附加
        for (List<String> list : data) {
            // 添加新的数据行
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : list) {
                stringBuffer.append(str);
                stringBuffer.append(",");
            }
            bw.write(stringBuffer.toString());
//            bw.write(StringUtils.join(list, ","));
            bw.newLine();
        }
        bw.close();
    }
}
